package com.baoxin.chenxixc4coach.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.bean.AudioCourse;

import java.util.List;

public class AudioAdapter extends RecyclerView.Adapter<AudioAdapter.ViewHolder> {

    private List<AudioCourse> audioList;
    private OnAudioEditListener audioEditListener;
    private OnAudioClickListener audioClickListener;

    public void setData(List<AudioCourse> audioList) {
        this.audioList = audioList;

    }

    public AudioAdapter(OnAudioEditListener audioEditListener, OnAudioClickListener audioClickListener) {
        this.audioEditListener = audioEditListener;
        this.audioClickListener = audioClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.audio_course_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final AudioCourse audio = audioList.get(position);
        holder.title.setText(audio.getAuName());

        holder.time.setText(audio.getAuTime());


        holder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                audioEditListener.onAudioEdit(position);
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                audioClickListener.onAudioClick(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return audioList == null ? 0 : audioList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        TextView title;
        TextView time;
        ImageView edit;

        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            title = itemView.findViewById(R.id.title);
            time = itemView.findViewById(R.id.time);
            edit = itemView.findViewById(R.id.edit);
        }
    }

    public interface OnAudioEditListener {
        void onAudioEdit(int pos);
    }

    public interface OnAudioClickListener {
        void onAudioClick(int pos);
    }

}
